package com.tjetc.usermanage;

import com.tjetc.domian.Communitycomplaint;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.ComplaintService;
import com.tjetc.vo.AppData;
import com.tjetc.vo.Result;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;


public class ComplaintView {
    private static Scanner scanner = new Scanner(System.in);
    private ComplaintService complaintService = ClientProxy.getProxyInstance(ComplaintService.class);
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");

    public void start() {
        while (true) {
            System.out.println("投诉菜单");
            System.out.println("1.查询投诉信息");
            System.out.println("2.添加投诉信息");
            System.out.println("3.修改投诉信息");
            System.out.println("4.删除投诉信息");
            System.out.println("0.返回上一级");
            int i = scanner.nextInt();
            switch (i) {
                case 1:
                    select();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    update();
                    break;
                case 4:
                    delete();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }
    }

    private void delete() {
        System.out.println("请输入要删除的投诉id");
        int complaintid = scanner.nextInt();
        Result result = complaintService.delete(complaintid);
        System.out.println(result.getMsg());
    }

    private void update() {
        System.out.println("修改投诉信息");
        System.out.println("请输入要修改的id");
        int complaintid = scanner.nextInt();
        System.out.println("请输入要修改的投诉日期 (格式: yyyy-MM-dd)");
        String complaintdate = scanner.next();
        System.out.println("请输入投诉时间(格式: HH:mm:ss)");
        String complainttime = scanner.next();
        System.out.println("请输入投诉描述");
        String description = scanner.next();
        try {
            Date date = dateFormat.parse(complaintdate);
            Date time = timeFormat.parse(complainttime);
            Communitycomplaint communitycomplaint = new Communitycomplaint();
            communitycomplaint.setComplaintid(complaintid);
            communitycomplaint.setComplaintdate(date);
            communitycomplaint.setComplainttime(time);
            communitycomplaint.setDescription(description);
            communitycomplaint.setStatus("待处理");
            Result result = complaintService.update(communitycomplaint);
            System.out.println(result.getMsg());
        } catch (ParseException e) {
            System.out.println("输入的日期或时间格式错误，请按照提示格式输入！");
        }
    }

    private void add() {
        System.out.println("添加投诉信息");
        System.out.println("请输入居民id");
        int residentid = scanner.nextInt();
        System.out.println("请输入投诉日期 (格式: yyyy-MM-dd)");
        String complainttime = scanner.next();
        System.out.println("请输入投诉时间(格式: HH:mm:ss)");
        String complaintdate = scanner.next();
        System.out.println("请输入投诉描述");
        String description = scanner.next();
        try {
            Date date = dateFormat.parse(complainttime);
            Date time = timeFormat.parse(complaintdate);
            Communitycomplaint communitycomplaint = new Communitycomplaint();
            communitycomplaint.setResidentid(residentid);
            communitycomplaint.setComplaintdate(date);
            communitycomplaint.setComplainttime(time);
            communitycomplaint.setDescription(description);
            communitycomplaint.setStatus("待处理");
            Result result = complaintService.add(communitycomplaint);
            System.out.println(result.getMsg());
        } catch (ParseException e) {
            System.out.println("输入的日期或时间格式错误，请按照提示格式输入！");
        }



    }
    private void select() {
        int residentId = AppData.getCurrentResidentId();//获取当前登录的居民id
        List<Communitycomplaint> list = complaintService.select(residentId);
        if (list == null){
            System.out.println("没有数据");
        }else{
            for (Communitycomplaint communitycomplaint : list) {
                System.out.println(communitycomplaint);
            }
        }

    }
}
